#encoding:utf-8 

import os, sys, time, string, copy
import oss2

class OssCtrl:
	HOST = "" 
	ACCESS_ID = "" 
	SECRET_ACCESS_KEY = "" 

	_shareoss = None
	_sharebucket = None

	@classmethod
	def shareoss(cls, HOST, ACCESS_ID , SECRET_ACCESS_KEY):	
		cls.HOST = HOST 
		cls.ACCESS_ID = ACCESS_ID 
		cls.SECRET_ACCESS_KEY = SECRET_ACCESS_KEY 

		if not cls._shareoss:
			auth = oss2.Auth(cls.ACCESS_ID, cls.SECRET_ACCESS_KEY)
			cls._shareoss = oss2.Service(auth, cls.HOST, connect_timeout=5)
		return cls._shareoss

	@classmethod
	def sharebkt(cls, buckname):
		if not cls._sharebucket:
			auth = oss2.Auth(cls.ACCESS_ID, cls.SECRET_ACCESS_KEY)
			cls._sharebucket = oss2.Bucket(auth, cls.HOST, buckname)

		return cls._sharebucket


	@classmethod
	def upload_batch(cls, bucket, filelist, tarpath, callback):
		uploadlist = copy.deepcopy(filelist)

		excure_dl = None
		def download_proce(prosize, maxsize):
			percent = float(prosize) / float(maxsize)
			percent_str = str(percent*100.0)
			if percent >= 1.0:
				print ("upload file done!")
				if len(uploadlist) == 0:
					print ("upload ALL done!")
					if hasattr(callback ,"__call__"):
						callback()
					
				else:
					excure_dl()

		def excure_dl():

			fileobj = uploadlist.pop()
			filename = ""
			if string.find("fileobj", os.sep):
				filename = string.split(fileobj, os.sep)[-1]
			else:
				filename = fileobj
			bucket.put_object_from_file(tarpath+filename, fileobj, progress_callback = download_proce)
		excure_dl()


#=======================================
#=======================================
# TEMPLATE

#inc oss
'''incoss = OssCtrl.shareoss()
'''

#inc bucket
'''incbkt = OssCtrl.sharebkt("dwb-private")
'''

#upload file to bucket
'''
def updateload_hook(prosize, maxsize):
 	print ("Upload percent is---->>>%s"%str(float(prosize) / float(maxsize)*100.0) + "%")
incbkt.put_object_from_file("史蒂夫•乔布斯传1.mobi", "史蒂夫•乔布斯传.mobi", progress_callback = updateload_hook)
'''


#uploaf string to bucket
'''incbkt.put_object("log.txt" ,"test1---->>upload string!")
'''

#delete obj 
'''incbkt.delete_object("史蒂夫•乔布斯传.mobi")
'''

#file exit?
'''incbkt.object_exists("史蒂夫•乔布斯传1.mobi")
'''

#upload batch
'''
# blist = [
# 	"C:\\Users\\wenbin\\Desktop\\test_oss\\oss_sdk\\1.mobi",
# 	"C:\\Users\\wenbin\\Desktop\\test_oss\\oss_sdk\\2.mobi",
# 	"C:\\Users\\wenbin\\Desktop\\test_oss\\oss_sdk\\3.mobi",
# 	"C:\\Users\\wenbin\\Desktop\\test_oss\\oss_sdk\\4.mobi"
# ]
# OssCtrl.upload_batch(incbkt, blist)
'''
#=======================================
#=======================================

incoss = OssCtrl.shareoss("oss-cn-beijing.aliyuncs.com", "I3qKdJMQC8TiuTCy", "MX0mTN960SezGAroN4Ldh1VgxSbf9g")
incbkt = OssCtrl.sharebkt("pylib")
# fileobj = incbkt.get_object("init.py")

# import chardet, string

# src = fileobj.read()
# src = src.encode("utf-8")

# import uuid 

# filename = string.replace(str(uuid.uuid4()),"-", '')

# fileobj = open(filename, 'r+')
# print (chardet.detect(src))
# fileobj.write(src)
# fileobj.close()
# src = eval(src)

# print (src)


# def endcallback():
# 	print ("callback")
# OssCtrl.upload_batch(incbkt, [
# 	"E:\\private_work\\git_work\\py_exlib\\exlib\\remote_ctrl.py",
# 	"E:\\private_work\\git_work\\py_exlib\\exlib\\remote_ctrl.pyc",
# 	], "test/", endcallback)